package com.ling.netty.demo10.server;

import com.ling.netty.demo10.client.MyInMsgHandler10;
import com.ling.netty.demo10.client.MyOutMsgHandler10;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

import java.nio.charset.Charset;

/**
 * @author ling007
 * @date 2024/8/5 14:45
 * @desciption:
 */
public class MyChannelInitializer10 extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel channel) throws Exception {
        // 基于换行符号
        channel.pipeline().addLast(new LineBasedFrameDecoder(1024));
        // 解码转String，注意调整自己的编码格式GBK、UTF-8
        channel.pipeline().addLast(new StringDecoder(Charset.forName("GBK")));
        // 解码转String，注意调整自己的编码格式GBK、UTF-8
        channel.pipeline().addLast(new StringEncoder(Charset.forName("GBK")));
        // 在管道中添加我们自己的接收数据实现方法
        channel.pipeline().addLast(new MyOutMsgHandler10());//消息出站处理器，在Client发送消息时候会触发此处理器
        channel.pipeline().addLast(new MyInMsgHandler10()); //消息入站处理器
    }
}
